<?php
/*
 * IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:810578553@qq.com
* @小麦 修改时间2014年3月25日 图片模型
*/
namespace IKPHP\Common\Models;

class Images extends BaseModel {
	
    public function getSource()
    {
        return IK."images";
    }	
	// 根据用户 和 类型
	public static function addImage($name, $path, $size, $title,$type, $typeid, $userid) {
		$photonum = self::count("typeid = '$typeid' and type = '$type' ");
		// 插入数据库
		$arrData = array (
				'seqid' => $photonum + 1,
				'userid' => $userid,
				'type' => $type,
				'typeid' => $typeid,
				'name' => $name,
				'title' => $title,
				'size' => $size,
				'align' => 'C',
				'path' => $path,
				'addtime' => time (),
		);
		$photo = new Images();
		if (false !== $photo->save( $arrData )){
			$photoid = $photo->id;
			return $photoid;
		}
		
	}
	//根据用户seqid 图片
	public static function getImageByseqid($type, $typeid, $seqid){
		$where  = array("type = '$type' and typeid='$typeid' and seqid = '$seqid'");
		$result = self::findFirst($where);
		if($result){
			$result = $result->toArray();
			$ext =  explode ( '.', $result['name']);
			//图片大小
			$result['simg'] =  attach($result['path'].$ext[0].'_'.C('ik_simg.width').'_'.C('ik_simg.height').'.'.$ext[1]);
			$result['mimg'] =  attach($result['path'].$ext[0].'_'.C('ik_mimg.width').'_'.C('ik_mimg.height').'.'.$ext[1]);
			$result['bimg'] =  attach($result['path'].$ext[0].'_'.C('ik_bimg.width').'_'.C('ik_bimg.height').'.'.$ext[1]);
			$result['img']  =  attach($result['path'].$ext[0].'.'.$ext[1]);
			return $result;			
		}else{
			return false;
		}
	}
	//根据用户photoid 图片
	public static function getImageById($photoid){
		$result = self::findFirst("id = '$photoid'")->toArray();
		$ext =  explode ( '.', $result['name']);
		//图片大小
		$result['simg'] =  attach($result['path'].$ext[0].'_'.C('ik_simg.width').'_'.C('ik_simg.height').'.'.$ext[1]);
		$result['mimg'] =  attach($result['path'].$ext[0].'_'.C('ik_mimg.width').'_'.C('ik_mimg.height').'.'.$ext[1]);
		$result['bimg'] =  attach($result['path'].$ext[0].'_'.C('ik_bimg.width').'_'.C('ik_bimg.height').'.'.$ext[1]);
		$result['img']  =  attach($result['path'].$ext[0].'.'.$ext[1]);
		return $result;
	}
	// 根据type typeid 获取图
	public static function getImagesByTypeid($type, $typeid){
		$arrImages = self::find(array("type = '$type' and typeid='$typeid'",'order'=>'seqid asc'));
		if(count($arrImages)>0){
			foreach($arrImages->toArray() as $item){
				$result[] = self::getImageById($item['id']);
			}		
			return $result;	
		}
		return false;
	}
	// 更新图片信息
	public static function updateImage($data,$where){
		$result = self::findFirst($where)->save($data);
		return $result;	
	}
	// 根据photoid删除图片
	public static function delImage($id){
		$result = self::findFirst("id = '$id'")->delete();
		if($result){
			return true;
		}else{
			return false;
		}
	}
	// 根据type typeid 删除所有图片
	public static function delAllImage($type, $typeid){
		$result = $this->where(array('type'=>$type,'typeid'=>$typeid))->delete();
		if($result){
			return true;
		}else{
			return false;
		}
	}
	// 根据map条件查询
	public static function getImagesByMap($map,$order='',$limit=''){
		
		!empty($map) && $arrImages = $this->where($map)->order($order)->limit($limit)->select();
		foreach($arrImages as $item){
			$result[] = $this->getImageById($item['id']);
		}
		return $result;
	}
	//统计数量
	public static function countImagesByMap($map){
		!empty($map) && $count = $this->where($map)->count('id');
		return $count;
	}

}